<template>
  <div class="data-table-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.param.dataObj" inline>
        <el-form-item class="!mr-4" prop="roleName">
          <el-input v-model="state.tableData.param.dataObj.roleName" class="max-w-180px" clearable
                    placeholder="请输入角色名称"
                    size="default" @keyup.enter.native="getTableData"></el-input>
        </el-form-item>
        <el-form-item v-auth="'role.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'role.add'" class="!mr-4">
          <el-button size="default" type="success" @click="onOpenDialog('add')">
            <el-icon>
              <ele-FolderAdd/>
            </el-icon>
            新增
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'role.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="onHandleResetFields">
            <el-icon>
              <ele-Refresh/>
            </el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" style="width: 100%">
        <el-table-column label="序号" type="index" width="60"/>
        <el-table-column label="角色名称" prop="roleName" show-overflow-tooltip></el-table-column>
        <el-table-column label="角色标识" prop="roleSign" show-overflow-tooltip></el-table-column>
        <el-table-column label="排序" prop="sort" show-overflow-tooltip></el-table-column>
        <el-table-column label="角色状态" prop="active" show-overflow-tooltip>
          <template #default="scope">
            <el-tag v-if="scope.row.active" type="success">启用</el-tag>
            <el-tag v-else type="info">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="角色描述" prop="remark" show-overflow-tooltip></el-table-column>
        <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button v-auth="'role.edit'" size="default" text type="primary" @click="onOpenDialog('edit', scope.row)">
              修改
            </el-button>
            <el-button v-auth="'role.assign'" size="default" text type="success"
                       @click="onOpenDialog('assign', scope.row)">分配权限
            </el-button>
            <el-button v-auth="'role.del'" size="default" text type="danger" @click="onRowDel(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:current-page="state.tableData.param.pageNum"
                     v-model:page-size="state.tableData.param.pageSize" :page-sizes="[10, 20, 30]" :pager-count="5"
                     :total="state.tableData.total" background class="mt15"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange">
      </el-pagination>
    </el-card>
    <RoleDialog ref="roleDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script lang="ts" name="systemRole" setup>
import {defineAsyncComponent, onMounted, reactive, ref} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import {roleApi} from '/@/api/sys/role';

// 引入组件
const RoleDialog = defineAsyncComponent(() => import('/@/views/system/role/dialog.vue'));

// 定义变量内容
const roleDialogRef = ref();
const queryFormRef = ref();
const state = reactive<SysRoleState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      search: '',
      pageNum: 1,
      pageSize: 10,
      dataObj: {
        roleName: ""
      }
    },
  },
});
// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  roleApi()
      .getTableData(state.tableData.param)
      .then((res) => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      });
};
// 打开弹窗
const onOpenDialog = (type: string, row?: Object) => {
  roleDialogRef.value.openDialog(type, row);
};
// 删除角色
const onRowDel = (row: RowRoleType) => {
  ElMessageBox.confirm(`此操作将永久删除角色名称：“${row.roleName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(() => {
        roleApi()
            .delRecord(row)
            .then(() => {
              getTableData();
              ElMessage.success('删除成功');
            });
      })
      .catch(() => {
      });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};
</script>
